public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    boolean isLeaf(TreeNode root) {
        if (root == null) {
            return false;
        }
        return root.left == null && root.right == null;
    }

    public int minCameraCover(TreeNode root) {
        return postorder(null, root);
    }

    int postorder(TreeNode parent, TreeNode cur) {
        if (cur == null) {
            return 0;
        }

        if (isLeaf(cur)) {
            if (parent == null) {
                return 1;
            }
            return 0;
        }

        int left = postorder(cur, cur.left);
        int right = postorder(cur, cur.right);
        if ((cur.left != null && cur.left.val == 0) || (cur.right != null && cur.right.val == 0)) {
            //安装摄像头
            cur.val = 1;
            if (parent != null) {
                parent.val = 1;
            }
            return left + right + 1;
        }
        if (parent == null && cur.val == 0) {
            return left + right + 1;
        }
        return left + right;
    }
}